package com.ysd.admin.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.ysd.dao.impl.LunBoTuDaoimpl;
import com.ysd.entity.LunBoTu;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;


public class AdminLunBoTuServlet extends BaseServlet {
	/**
	 * localhost:8080/xiyangyangchaoshi/adminlunbotu?reqName=selLunBoTu
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void selLunBoTu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		LunBoTuDaoimpl lbtDao=new LunBoTuDaoimpl();
		int page=Integer.parseInt(request.getParameter("page"));
		int size=Integer.parseInt(request.getParameter("size"));
		ArrayList<LunBoTu> lbtList=lbtDao.getLunBoTu(page,size);
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		Gson gson=new Gson();
		out.print(gson.toJson(lbtList));
		
		out.flush();
		out.close();
	}
	
	/**
	 * localhost:8080/xiyangyangchaoshi/adminlunbotu?reqName=selLunBoTuSize
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void selLunBoTuSize(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		LunBoTuDaoimpl lbtDao=new LunBoTuDaoimpl();
		ArrayList<LunBoTu> lbtList=lbtDao.getLunBoTuSize();
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		Gson gson=new Gson();
		out.print(gson.toJson(lbtList.size()));
		
		out.flush();
		out.close();
	}
	/**
	 * localhost:8080/xiyangyangchaoshi/adminlunbotu?reqName=delLunBoTu
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delLunBoTu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		LunBoTuDaoimpl lbtDao=new LunBoTuDaoimpl();
		int lunBoTuId=Integer.parseInt(request.getParameter("lunBoTuId"));
		int n=lbtDao.delLunBoTu(lunBoTuId);
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		if(n>0) {
			out.print(Result.toClient(true, "删除成功", ""));
		}else {
			out.print(Result.toClient(false, "删除失败", "您的操作有误"));
		}
	
		
		out.flush();
		out.close();
	}
	
	
	

	/**
	 * localhost:8080/xiyangyangchaoshi/adminlunbotu?reqName=addLunBoTu
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addLunBoTu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		LunBoTuDaoimpl lbtDao=new LunBoTuDaoimpl();
		String lunbotu=request.getParameter("lunbotu");
		if(lunbotu!=null && !lunbotu.equals("")) {
			ArrayList<LunBoTu> lbtList=lbtDao.selLunBoTuByName(lunbotu);
			if(lbtList.size()==0) {
				LunBoTu lunBoTu=new LunBoTu(0, lunbotu);
				int n=lbtDao.addLunBoTu(lunBoTu);
				if(n>0) {
					out.print(Result.toClient(true, "添加成功", ""));
				}else {
					out.print(Result.toClient(false, "添加失败", "您的操作有误"));
				}
			}else {
				out.print(Result.toClient(false, "图片已存在", "您的操作有误"));
			}
		}
		out.flush();
		out.close();
	}
	
	
	
	/**
	 * localhost:8080/xiyangyangchaoshi/adminlunbotu?reqName=selLunBoTuById
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void selLunBoTuById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		LunBoTuDaoimpl lbtDao=new LunBoTuDaoimpl();
		int lbtid=Integer.parseInt(request.getParameter("lbtid"));
		LunBoTu lbtList=lbtDao.selLunBoTuById(lbtid);
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		Gson gson=new Gson();
		out.print(gson.toJson(lbtList));
		
		out.flush();
		out.close();
	}
	
	
	/**
	 * localhost:8080/xiyangyangchaoshi/adminlunbotu?reqName=editlunBoTuById
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void editlunBoTuById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		LunBoTuDaoimpl lbtDao=new LunBoTuDaoimpl();
		int fenleiId=Integer.parseInt(request.getParameter("fenleiId"));
		String lunbotu=request.getParameter("lunbotu");
		if(lunbotu!=null && !lunbotu.equals("")) {
			ArrayList<LunBoTu> lbtList=lbtDao.selLunBoTuByName(lunbotu);
			if(lbtList.size()==0) {
				LunBoTu lunBoTu=new LunBoTu(fenleiId, lunbotu);
				int n=lbtDao.editlunBoTuById(lunBoTu);
				if(n>0) {
					out.print(Result.toClient(true, "修改成功", ""));
				}else {
					out.print(Result.toClient(false, "修改失败", "您的操作有误"));
				}
			}else {
				out.print(Result.toClient(false, "图片已存在", "您的操作有误"));
			}
		}
		out.flush();
		out.close();
	}
/**
 * 	localhost:8080/xiyangyangchaoshi/adminlunbotu?reqName=upload
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 */
public void upload(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter out=response.getWriter();
		
		SmartUpload smart=new  SmartUpload();
		smart.setCharset("UTF-8");
		smart.initialize(getServletConfig(), request, response);
		
		try {
			smart.upload();
			String name=System.currentTimeMillis()+".jpg";
			smart.getFiles().getFile(0).saveAs("/images/"+name);
			out.print(Result.toClient(true, "上传成功", name));
		} catch (SmartUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			out.print(Result.toClient(false, "上传失败", "图片过大"));
		}
		
		out.flush();
		out.close();
}	
	
	
	
}
